//
//  ZBSegScrollview.m
//  MoBan
//
//  Created by apple on 16/7/22.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ZBSegScrollview.h"
@interface ZBSegScrollview()
{
    NSMutableArray *_array;
    NSInteger _ScreenSubviewsCount;
}

/**
 下面的滑动的线
 */
@property(nonatomic,strong)UIImageView *lineImageView;
/**
 盛放UI的数组
 */
@property(nonatomic,strong)NSMutableArray *uiarray;



/**
 字体
 */
@property(nonatomic,strong)UIFont *font;
/**
 正常的颜色
 */
@property(nonatomic,strong)UIColor *normalColor;
/**
 选中的颜色
 */
@property(nonatomic,strong)UIColor *selectColor;
/**
 下面的线的颜色
 */
@property(nonatomic,strong)UIColor *lineColor;
@end
@implementation ZBSegScrollview
-(NSMutableArray *)uiarray
{
    if (!_uiarray) {
        _uiarray = [NSMutableArray array];
    }
    return _uiarray;
}
-(NSMutableArray *)array
{
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSArray *array1 = @[@"第一个",@"第二个",@"第三个",@"第四个",@"第五个",@"第六个",@"第七个"];
        [self.array addObjectsFromArray:array1];
        self.font = [UIFont systemFontOfSize:13];
        self.normalColor = [UIColor blackColor];
        self.selectColor = [UIColor redColor];
        self.ScreenSubviewsCount = 5;
        self.lineColor = [UIColor redColor];
        [self AddUI];
    }
    return self;
}
-(void)AddUI
{
    
    [self.uiarray removeAllObjects];
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    for (NSInteger i=0; i<self.array.count; i++) {
        
        UIButton *butten = [[UIButton alloc]init];
        [butten setTitle:self.array[i] forState:UIControlStateNormal];
        butten.titleLabel.font =self.font;
        [butten setTitleColor:self.normalColor forState:UIControlStateNormal];
        [butten setTitleColor:self.selectColor forState:UIControlStateSelected];
        butten.tag = i;
        [butten addTarget:self action:@selector(BtnClic:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:butten];
        [self.uiarray addObject:butten];
        
    }
    self.lineImageView = [[UIImageView alloc]init];
    self.lineImageView.backgroundColor =self.lineColor;
    self.lineImageView.frame = CGRectMake(0,self.frame.size.height-3, self.frame.size.width/self.ScreenSubviewsCount, 3 );
    [self addSubview:self.lineImageView];
//    [self ZBChangeFramWith:0];
    
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat btnW = self.frame.size.width/self.ScreenSubviewsCount;
    CGFloat btnH = self.frame.size.height-3;
    for (NSInteger i=0; i<self.uiarray.count; i++) {
        UIButton *butten = self.uiarray[i];
        butten.frame = CGRectMake(btnW*i, 0, btnW, btnH);
    }
    self.contentSize = CGSizeMake(btnW*self.uiarray.count, self.frame.size.height);
}

/**
 butten的点击
 */
-(void)BtnClic:(UIButton *)butten
{
    [self ZBChangeFramWith:butten.tag];
    
    if ([self.deleget respondsToSelector:@selector(ZBSegScrollview:didClicAtIndex:)]) {
        [self.deleget ZBSegScrollview:self didClicAtIndex:butten.tag];
    }

}
/**
 根据butten的点击计算线的移动
 */
-(void)ZBChangeFramWith:(NSInteger)inter;
{
    CGFloat lineW = self.frame.size.width/self.ScreenSubviewsCount;
    [UIView animateWithDuration:0.5 animations:^{
    self.lineImageView.frame = CGRectMake(lineW*inter, self.frame.size.height-3, lineW, 3);
    }];

}
/**
 根据传入的x的值计算下面的线移动的多少
 */
-(void)ZBScrollviewSetLineViewX:(CGFloat)scrollviewX
{
    
    
    
    self.lineImageView.frame = CGRectMake(scrollviewX/self.ScreenSubviewsCount, self.frame.size.height-3, self.frame.size.width/self.ScreenSubviewsCount, 3);
    if (scrollviewX/KscrW>self.ScreenSubviewsCount/2&&scrollviewX/KscrW<self.array.count-1-self.ScreenSubviewsCount/2) {
        self.contentOffset = CGPointMake((self.frame.size.width/self.ScreenSubviewsCount)*(scrollviewX/KscrW-self.ScreenSubviewsCount/2), 0);
    }
    
    

}
-(void)setArray:(NSMutableArray *)array
{
    _array = array;
    [self AddUI];
}
-(void)setScreenSubviewsCount:(NSInteger)ScreenSubviewsCount
{
    _ScreenSubviewsCount = ScreenSubviewsCount;
    [self AddUI];
}
@end
